package com.shandao.shangxiu.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.shandao.shangxiu.bean.entity.BeanCountry;
import com.shandao.shangxiu.bean.entity.CheckCountry;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2015/7/9.
 */
public class DbManager {
    private static DbHelper helper;
    private SQLiteDatabase db;

    public DbManager(Context context) {
        if (helper == null) {
            helper = new DbHelper(context);
        }
    }

    /**
     * 初始化城市列表数据
     */
    public void insertCities(List<BeanCountry> country) {
        db = helper.getWritableDatabase();
        db.beginTransaction();
        try {
            for (BeanCountry countryEntry : country) {
                ContentValues values = new ContentValues();
                values.put(CountryEntry.COLUMN_PROVINCE, countryEntry.province);
                values.put(CountryEntry.COLUMN_CITY, countryEntry.city);
                values.put(CountryEntry.COLUMN_DISTRICT, countryEntry.district);
                values.put(CountryEntry.COLUMN_PROVINCE_CODE, countryEntry.provinceCode);
                values.put(CountryEntry.COLUMN_CITY_CODE, countryEntry.cityCode);
                values.put(CountryEntry.COLUMN_DISTRICT_CODE, countryEntry.districtCode);
                values.put(CountryEntry.COLUMN_ACRONYM_NAME, countryEntry.acronymName);
                values.put(CountryEntry.COLUMN_ACRONYM, countryEntry.acronym);
                db.replace(CountryEntry.TABLE_NAME, null, values);
            }
            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
            db.close();
        }
    }


    /**
     * 查询所有城市，用于城市排序列表
     */
    public List<BeanCountry> queryAllCities() {
        db = helper.getReadableDatabase();
        db.beginTransaction();
        List<BeanCountry> datas = new ArrayList<>();
        String[] columns = {CountryEntry.COLUMN_ACRONYM_NAME, CountryEntry.COLUMN_ACRONYM};
        String whereCls =  CountryEntry.COLUMN_ORIGINAL_CODES + " != ''";
        String orderBy = CountryEntry.COLUMN_ACRONYM + " ASC ";
        Cursor cursor = db.query(true, CountryEntry.TABLE_NAME, columns, whereCls, null, null, null,
                orderBy, null);
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            BeanCountry beanCountry = new BeanCountry();
            beanCountry.acronymName = cursor.getString(cursor.getColumnIndex(CountryEntry
                    .COLUMN_ACRONYM_NAME));
            beanCountry.acronym = cursor.getString(cursor.getColumnIndex(CountryEntry
                    .COLUMN_ACRONYM));
            datas.add(beanCountry);
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        cursor.close();
        db.close();
        return datas;
    }



    /**
     * 根据省份查询下级市，用于地址管理
     */
    public List<CheckCountry> queryCity(String provinceCode) {
        db = helper.getReadableDatabase();
        db.beginTransaction();
        List<CheckCountry> datas = new ArrayList<>();
        String[] columns = {CountryEntry.COLUMN_CITY, CountryEntry.COLUMN_CITY_CODE};
        String selection = CountryEntry.COLUMN_PROVINCE_CODE + " = ?";
        String[] selectionArgs = {provinceCode};
        Cursor cursor = db.query(true, CountryEntry.TABLE_NAME, columns, selection,
                selectionArgs, null, null, null, null);
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            CheckCountry checkCountry = new CheckCountry();
            checkCountry.name = cursor.getString(cursor.getColumnIndex(CountryEntry.COLUMN_CITY));
            checkCountry.code = cursor.getString(cursor.getColumnIndex(CountryEntry
                    .COLUMN_CITY_CODE));
            datas.add(checkCountry);
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        cursor.close();
        db.close();
        return datas;
    }

    /**
     * 根据市查询下级县区，用于地址管理l
     */
    public List<CheckCountry> queryDistrict(String cityCode) {
        db = helper.getReadableDatabase();
        db.beginTransaction();
        List<CheckCountry> datas = new ArrayList<>();
        String[] columns = {CountryEntry.COLUMN_DISTRICT, CountryEntry.COLUMN_DISTRICT_CODE};
        String selection = CountryEntry.COLUMN_CITY_CODE + " = ? ";
        String[] selectionArgs = {cityCode};
        Cursor cursor = db.query(true, CountryEntry.TABLE_NAME, columns, selection,
                selectionArgs, null, null, null, null);
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            CheckCountry checkCountry = new CheckCountry();
            checkCountry.name = cursor.getString(cursor.getColumnIndex(CountryEntry
                    .COLUMN_DISTRICT));
            checkCountry.code = cursor.getString(cursor.getColumnIndex(CountryEntry
                    .COLUMN_DISTRICT_CODE));
            datas.add(checkCountry);
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        cursor.close();
        db.close();
        return datas;
    }

}
